package handlers

import (
	"encoding/json"
	"repo-man/cache"
	"repo-man/models"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_address := c.PostForm("good_address")
	data := models.Good{
		GoodCode:    good_code,
		GoodName:    good_name,
		GoodAddress: good_address,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func GoodOrderAdd(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	good_order_code := c.PostForm("good_order_code")
	good_order_price := c.PostForm("good_order_price")
	good_order_person := c.PostForm("good_order_person")
	data := models.GoodOrder{
		Gid:             gid,
		GoodOrderCode:   good_order_code,
		GoodOrderPrice:  good_order_price,
		GoodOrderPerson: good_order_person,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func GoodOrderList(c *gin.Context) {
	/*
		SELECT
		  good_orders.id AS GoodOrderId,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_address AS GoodAddress,
		  good_orders.good_order_code AS GoodOrderCode,
		  good_orders.good_order_price AS GoodOrderPrice,
		  good_orders.good_order_person AS GoodOrderPerson
		FROM
		  good_orders
		  LEFT JOIN goods ON good_orders.gid = goods.id
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.GoodOrderList
	redis_key := "good_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select(
				"good_orders.id AS GoodOrderId",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_address AS GoodAddress",
				"good_orders.good_order_code AS GoodOrderCode",
				"good_orders.good_order_price AS GoodOrderPrice",
				"good_orders.good_order_person AS GoodOrderPerson",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Offset(offset).Limit(size).Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
		"msg":   "ok",
	})
	return

}
func GoodOrderDetail(c *gin.Context) {
	/*
		SELECT
		  good_orders.id AS GoodOrderId,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_address AS GoodAddress,
		  good_orders.good_order_code AS GoodOrderCode,
		  good_orders.good_order_price AS GoodOrderPrice,
		  good_orders.good_order_person AS GoodOrderPerson
		FROM
		  good_orders
		  LEFT JOIN goods ON good_orders.gid = goods.id
	*/
	id := c.Query("id")
	var list models.GoodOrderList
	redis_key := "good_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select(
				"good_orders.id AS GoodOrderId",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_address AS GoodAddress",
				"good_orders.good_order_code AS GoodOrderCode",
				"good_orders.good_order_price AS GoodOrderPrice",
				"good_orders.good_order_person AS GoodOrderPerson",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Where("good_orders.id=?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "ok",
	})
	return

}
